﻿using MyAspx.EasyFramework.DAL;
using MyAspx.EasyFramework.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebUI.Services.waterfall
{

    public class DataFit {

        public DataFit()
        {


        }
        public string FN{get;set;}
        public string IMG { get; set; }
        public string FID { get; set; }
                public decimal? SALENUM{get;set;}
    }
    /// <summary>
    /// pubuliu 的摘要说明
    /// </summary>
    public class pubuliu : IHttpHandler
    {
        DBAccess db = new DBAccess();
        public commyaspxdbEntities _commyaspxdbEntities = new commyaspxdbEntities();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/Json";

            string action = context.Request.QueryString["action"];
            if (string.IsNullOrEmpty(action))
            {
                return;
            }
            else
            {
                switch (action)
                {
                    case "pubuliu":
                        //string json = TestPage(context);

                        if (!string.IsNullOrEmpty(context.Request.QueryString["page"]))
                        {
                            if (Convert.ToInt32(context.Request.QueryString["page"]) > 10)
                            {
                                context.Response.Write("[]");
                                return;
                            }
                        }
                        string json = string.Empty;
                       var adddd = _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).Count();

                        if (context.Request.QueryString["op"] == "addtime")
                        {
                            if (context.Request.QueryString["sort"] == "desc")
                            {
                                for (int i = 1; i < _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).Count(); i++)
                                {
                                    decimal? fs = 0;
                                    var filename = _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.addtime).Skip(i).FirstOrDefault().filename;
                                    if (_commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.addtime).Skip(i).FirstOrDefault().fileid).Count() != 0)
                                    {

                                        fs = _commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.addtime).Skip(i).FirstOrDefault().fileid).FirstOrDefault().salenum;
                                    }

                                    if (context.Request.QueryString["max"] != null || context.Request.QueryString["min"] != null)
                                    {
                                        if (fs >= decimal.Parse(context.Request.QueryString["min"].ToString()) && fs <= decimal.Parse(context.Request.QueryString["max"].ToString()))
                                        {
 json += "{";
                                        json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.addtime).Skip(i).FirstOrDefault().filefirimg);
                                        json += string.Format("\"height\":\"{0}\",", i);
                                        json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                        json += "},";
                                        }
                                       
                                    }
                                    else { 
                                    json += "{";
                                    json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.addtime).Skip(i).FirstOrDefault().filefirimg);
                                    json += string.Format("\"height\":\"{0}\",", i);
                                    json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderByDescending(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                    json += "},";}
                                }
                            }
                            else
                            {
                                for (int i = 1; i < _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).Count(); i++)
                                {
                                    decimal? fs = 0;
                                    var filename = _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.addtime).Skip(i).FirstOrDefault().filename;
                                    if (_commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.addtime).Skip(i).FirstOrDefault().fileid).Count() != 0)
                                    {

                                        fs = _commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.addtime).Skip(i).FirstOrDefault().fileid).FirstOrDefault().salenum;
                                    }

                                    if (context.Request.QueryString["max"] != null || context.Request.QueryString["min"] != null)
                                    {
                                        if (fs >= decimal.Parse(context.Request.QueryString["min"].ToString()) && fs <= decimal.Parse(context.Request.QueryString["max"].ToString()))
                                        {
                                            json += "{";
                                            json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.addtime).Skip(i).FirstOrDefault().filefirimg);
                                            json += string.Format("\"height\":\"{0}\",", i);
                                            json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                            json += "},";
                                        }

                                    }
                                    else
                                    {
                                        json += "{";
                                        json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.addtime).Skip(i).FirstOrDefault().filefirimg);
                                        json += string.Format("\"height\":\"{0}\",", i);
                                        json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                        json += "},";
                                    }
                                }
                            }

                        }
                        else if (context.Request.QueryString["op"] == "salenum")
                        {
                            List <DataFit> cur = new List<DataFit>();
                      
                            foreach (var a in _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3))
                            {

                             
                                cur.Add(new DataFit {
                                FN = a.filename,
                                IMG= a.filefirimg,
                                FID = a.fileid,
                                SALENUM = _commyaspxdbEntities.my_saledetail.Where(f => f.fileid == a.fileid).Count()<=0?0: _commyaspxdbEntities.my_saledetail.Where(f => f.fileid == a.fileid).FirstOrDefault().salenum
                                });
                            }
                            if (context.Request.QueryString["sort"] == "desc")
                            {
                                cur = cur.OrderByDescending(a => a.SALENUM).ToList();
                            }
                            else
                            {
                                cur = cur.OrderBy(a => a.SALENUM).ToList(); ;
                            }
                            for (int i = 1; i < _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).Count(); i++)
                            {
                                decimal? fs = 0;
                                var filename = cur[i].FN;
                                //if (cur.Count() != 0)
                                //{

                                //    fs = _commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid).FirstOrDefault().salenum;
                                //}

                                if (context.Request.QueryString["max"] != null || context.Request.QueryString["min"] != null)
                                {

                                    if (cur[i].SALENUM >= decimal.Parse(context.Request.QueryString["min"].ToString()) && cur[i].SALENUM <= decimal.Parse(context.Request.QueryString["max"].ToString()))
                                    {
                                        json += "{";
                                        json += string.Format("\"img\":\"{0}\",", cur[i].IMG);
                                        json += string.Format("\"height\":\"{0}\",", i);
                                        json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, cur[i].SALENUM, cur[i].FID);
                                        json += "},";
                                    }
                                }
                                else
                                {
                                    json += "{";
                                    json += string.Format("\"img\":\"{0}\",", cur[i].IMG);
                                    json += string.Format("\"height\":\"{0}\",", i);
                                    json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, cur[i].SALENUM, cur[i].FID);
                                    json += "},";
                                }
                              
                            }
                        }
                        else
                        {
                            for (int i = 1; i < _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).Count(); i++)
                            {
                                decimal? fs = 0;
                                var filename = _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().filename;
                                if (_commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid).Count() != 0)
                                {

                                    fs = _commyaspxdbEntities.my_saledetail.Where(c => c.fileid == _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid).FirstOrDefault().salenum;
                                }

                                if (context.Request.QueryString["max"] != null || context.Request.QueryString["min"] != null)
                                {
                                    if (fs >= decimal.Parse(context.Request.QueryString["min"].ToString()) && fs <= decimal.Parse(context.Request.QueryString["max"].ToString()))
                                    { 
                                    
                                        json += "{";
                                json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().filefirimg);
                                json += string.Format("\"height\":\"{0}\",", i);
                                json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                json += "},";
                                    
                                    }
                                }
                                else
                                {
                                    json += "{";
                                    json += string.Format("\"img\":\"{0}\",", _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().filefirimg);
                                    json += string.Format("\"height\":\"{0}\",", i);
                                    json += string.Format("\"title\":\"源码名称：<a target='_blank' href='../form/detailcode.aspx?guidstr={2}' ><font style='color:green; font-size:14px;'>{0}</font></a><br/> <font style='color:red; font-size:40px;'>{1}</font> /人民币\"", filename, fs, _commyaspxdbEntities.my_fileinfo.Where(f => f.saletypeid == 3).OrderBy(a => a.fileid).Skip(i).FirstOrDefault().fileid);
                                    json += "},";
                                }
                            
                            }
                        }
                        if (json.Trim() != "")
                        {
json = json.Substring(0, json.Length - 1);

                        }
                        else
                        {
                            json = "没有找到相关源码！";
                        }
                        context.Response.Write("[" + json + "]");
                        context.ApplicationInstance.CompleteRequest();
                        break;
                    default:
                        context.Response.Write("");
                        context.ApplicationInstance.CompleteRequest();
                        return;
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}